﻿using Anonym.Common;
using Anonym.Models.Common;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Anonym.MVC.Filter
{
    public class ExceptionFilter : IAsyncExceptionFilter
    {
        public Task OnExceptionAsync(ExceptionContext context)
        {
            if (context.ExceptionHandled == false)
            {
                //context.Result = new JsonResult(new ApiCustomResult { Success = false, Message = context.Exception.Message, Data = "", Path = context.HttpContext.Request.Path, RequestTime = DateTime.Now });
                //context.HttpContext.Response. (context.Exception.Message);
                Error error = new Error()
                {
                    Msg = context.HttpContext.Request.Path + "----" + context.Exception.Message,
                    Code = 0,
                    Url1 = "/home/login",
                    UrlStr = "出现错误了",
                    Target = "_top",
                };
                context.HttpContext.Session.SetObject("Error", error);
                context.Result = new RedirectResult("/Home/Error/");
            }
            context.ExceptionHandled = true;

            return Task.CompletedTask;
        }
    }
}
